const shareUtil = require('../../../utils/shareUtil.js');
Page({
    data: {
        penMenuImg: "pen-size",
        colorMenuImg: "pen-color",
        clearMenuImg: "clear",
        firstMenuFlag: false,
        penMenuFlag: true,
        colorMenuFlag: true,
        pen: 3,
        color: "#cc0033"
    },
    pointArray: "|",
    arrayIndex: 0,
    myCanvasWidth: 0,
    myCanvasHeight: 0,
    canvasWidth: 0,
    canvasHeight: 0,
    startX: 0,
    startY: 0,
    isClear: false,
    onLoad: function(t) {
        wx.setNavigationBarTitle({
            title: "绘画板"
        });
    },
    onShareAppMessage: function(t) {},
    touchStart: function(t) {
        this.startX = t.changedTouches[0].x, this.startY = t.changedTouches[0].y, this.context = wx.createContext(), 
        this.isClear ? (this.context.setStrokeStyle("#F8F8F8"), this.context.setLineCap("round"), 
        this.context.setLineJoin("round"), this.context.setLineWidth(20), this.context.save(), 
        this.context.beginPath(), this.context.arc(this.startX, this.startY, 5, 0, 2 * Math.PI, true), 
        this.context.fill(), this.context.restore()) : (this.context.setStrokeStyle(this.data.color), 
        this.context.setLineWidth(this.data.pen), this.context.setLineCap("round"), this.context.beginPath());
    },
    touchMove: function(t) {
        var e = t.changedTouches[0].x, a = t.changedTouches[0].y;
        this.isClear ? (this.context.save(), this.context.moveTo(this.startX, this.startY), 
        this.context.lineTo(e, a), this.context.stroke(), this.context.restore(), this.startX = e, 
        this.startY = a) : (this.context.moveTo(this.startX, this.startY), this.context.lineTo(e, a), 
        this.context.stroke(), this.startX = e, this.startY = a), wx.drawCanvas({
            canvasId: "myCanvas",
            reserve: true,
            actions: this.context.getActions()
        });
    },
    touchEnd: function() {},
    clearCanvas: function() {
        this.isClear ? (this.isClear = false, this.setData({
            clearMenuImg: "clear"
        })) : (this.isClear = true, this.setData({
            clearMenuImg: "clear-select"
        }));
    },
    saveCanvas: function() {
        wx.canvasToTempFilePath({
            canvasId: "myCanvas",
            success: function(e) {
                console.log(e.tempFilePath), t(e);
            },
            fail: function() {
                console.log("fail-downloadFile");
            }
        });
        var t = function(t) {
            console.log("保存图片：", t), wx.saveImageToPhotosAlbum({
                filePath: t.tempFilePath,
                success: function(t) {
                    console.log(t), wx.showToast({
                        title: "保存成功",
                        icon: "success"
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        };
    },
    penSelect: function(t) {
        this.setData({
            pen: parseInt(t.currentTarget.dataset.param),
            firstMenuFlag: false,
            penMenuFlag: true,
            colorMenuFlag: true,
            clearMenuImg: "clear"
        }), this.isClear = false;
    },
    colorSelect: function(t) {
        console.log(t.currentTarget), this.setData({
            color: t.currentTarget.dataset.param,
            firstMenuFlag: false,
            penMenuFlag: true,
            colorMenuFlag: true,
            clearMenuImg: "clear"
        }), this.isClear = false;
    },
    menuSelect: function(t) {
        "pen" === t.currentTarget.dataset.menu && this.setData({
            firstMenuFlag: true,
            penMenuFlag: false
        }), "color" === t.currentTarget.dataset.menu && this.setData({
            firstMenuFlag: true,
            colorMenuFlag: false,
            clearMenuImg: "clear"
        }), this.isClear = false;
    },
    menuBack: function(t) {
        console.log(t.currentTarget), "pen" === t.currentTarget.dataset.menu && this.setData({
            firstMenuFlag: false,
            penMenuFlag: true
        }), "color" === t.currentTarget.dataset.menu && this.setData({
            firstMenuFlag: false,
            colorMenuFlag: true,
            clearMenuImg: "clear"
        }), this.isClear = false;
    },
    onShareAppMessage:shareUtil.shareConfig,
    onShareTimeline:shareUtil.shareConfig,
});